var ns, stoptime, leftElem, popup;
var Vheight = window.clientHeight;
app = new Vue({
    el: '#app',
    data: {
        form: {
            status: 1,
            ischeckpage: false,
            pageindex: 0,
            pagesize: 10,
        },
        countform: {
            unreadcount: 20
        },
        list: [],
        current_item_length: 0,
        scroll_top: 0,
        can_scroll: false,
        showbg: false
    },
    methods: {
        get_data: function() {
            var that = this;
            var options = {};
            options.action = "getpushmsglist";
            options.pagesize = that.form.pagesize;
            options.pageindex = that.form.pageindex;
            options.status = that.form.status;
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    api.sendEvent({
                        name: 'reloadunreadmsgcount'
                    });
                    if (data.list.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.list = data.list;
                    } else {
                        that.list = that.list.concat(data.list);
                    }
                    app.current_item_length = that.list.length
                    app.countform.unreadcount = data.unreadcount;
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        select_status: function(status) {
            var that = this;
            that.form.status = status;
            that.form.pageindex = 0;
            that.get_data();
        },
        openGongdan: function(item) {
            ns.openWin('home_task_con_frm', '任务', {
                id: item.RelatedID
            });
        },
        openWin: function(item, params) {
            var that = this;
            if (item.url == '') {
                return;
            }
            params = params || {
                status: -1
            };
            ns.openWin(item.url, item.name, params);
        },
        open_mail: function() {
            ns.openWin('home_news_mail_frm', '通讯录', {
                mail: 5
            });
        },
        read_all: function(ID) {
            var that = this;
            var options = {};
            options.action = "readpushmsg";
            options.ID = 0;
            ns.post(options, function(succeed, data, err) {
                api.sendEvent({
                    name: 'reloadunreadmsgcount'
                });
                if (succeed) {
                    setTimeout(function() {
                        that.form.pageindex = 0;
                        that.get_data();
                    }, 500);
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        get_read_state: function(index) {
            var that = this;
            if (that.form.status == 2) {
                return;
            }
            var options = {};
            options.action = "gettotalundreadpushmsgcount";
            options.onlymsg = 1;
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    that.countform.unreadcount = data.count;
                }
            });
            that.list.splice(index, 1);
        },
        read_pushmsg: function(item, index, canopen) {
            var that = this;
            item.showbtn = false;
            that.showbg = item.showbtn;
            var options = {};
            options.action = "readpushmsg";
            options.ID = item.ID;
            options.MsgType = item.MsgType;
            ns.post(options, function(succeed, data, err) {
                api.sendEvent({
                    name: 'reloadunreadmsgcount'
                });
                if (succeed) {
                    that.get_read_state(index);
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
                if (!canopen) {
                    return;
                }
                if (item.MsgType == 101 || item.MsgType == 102 || item.MsgType == 103 || item.MsgType == 104 || item.MsgType == 105 || item.MsgType == 106) {
                    ns.openWin('home_task_con_frm', '工单详情', {
                        id: item.RelatedID
                    });
                    return;
                }
                if (item.MsgType == 201 || item.MsgType == 202 || item.MsgType == 203) {
                    ns.openWin('qualitycontroldetail_frm', '工单详情', {
                        id: item.RelatedID
                    });
                    return;
                }
                if (item.MsgType == 301) {
                    ns.openWin('tongzhixiaoxidetail_frm', '通知公告', {
                        id: item.RelatedID
                    });
                    return;
                }
                if (item.MsgType == 401) {
                    var url = 'equipment_list_frm';
                    var title = '任务开始';
                    if (data.TaskStatus == 1) {
                        url = 'equipment_summary_frm';
                        title = '任务详情';
                    }
                    ns.openWin(url, title, {
                        id: item.RelatedID
                    });
                    return;
                }
                if (item.MsgType == 501) {
                    var url = 'patrolplan_list_frm';
                    var title = '任务开始';
                    if (data.TaskStatus == 1) {
                        url = 'patrolplan_summary_frm';
                        title = '任务详情';
                    }
                    ns.openWin(url, title, {
                        id: item.RelatedID
                    });
                    return;
                }
                if (item.MsgType == 601) {
                    var url = 'bjlh_list_frm';
                    var title = '任务开始';
                    if (data.TaskStatus == 1) {
                        url = 'bjlh_summary_frm';
                        title = '任务详情';
                    }
                    ns.openWin(url, title, {
                        id: item.RelatedID
                    });
                    return;
                }
                api.toast({
                    msg: '暂未开放该类型',
                    duration: 2000,
                    location: 'bottom'
                });
            })
        },
        is_checkpage_changeStyle: function() {
            var that = this;
            if (that.form.ischeckpage == true) {
                var el = $api.byId('smart');
                $api.css(el, 'margin-top:0;');
            }
        },
        openWin: function(item, params) {
            var that = this;
            if (item.url == '') {
                return;
            }
            params = params || {
                status: -1
            };
            ns.openWin(item.url, item.name, params);
        },
        do_showbtn: function(item) {
            var that = this;
            for (var i = 0; i < that.list.length; i++) {
                that.list[i].showbtn = false;
            }
            item.showbtn = !item.showbtn;
            that.showbg = item.showbtn;
        },
        do_remove: function(item, index) {
            var that = this;
            api.confirm({
                title: '提示',
                msg: '确认删除?',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                if (ret.buttonIndex == 1) {
                    var options = {};
                    options.action = "doremovepushmsg";
                    options.ID = item.ID;
                    ns.post(options, function(succeed, data, err) {
                        api.sendEvent({
                            name: 'reloadunreadmsgcount'
                        });
                        that.list.splice(index, 1);
                    })
                }
            });
            item.showbtn = false;
            that.showbg = item.showbtn;
        },
        hide_bg: function() {
            var that = this;
            that.showbg = false;
            for (var i = 0; i < that.list.length; i++) {
                that.list[i].showbtn = false;
            }
        }
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.form.ischeckpage = api.pageParam.ischeckpage || false;
    ns.init_pull();
    app.is_checkpage_changeStyle();
    app.get_data();
    api.addEventListener({
        name: 'ajpushnotupdate'
    }, function(ret, err) {
        if (ret) {
            app.get_data();
        } else {}
    });
    api.addEventListener({
        name: 'home_new_updata'
    }, function(ret, err) {
        if (ret) {
            app.get_data();
        } else {}
    });
    api.addEventListener({
        name: 'do_read_all_msg'
    }, function(ret, err) {
        app.read_all();
    });
};
